<?php
global $_RQ,$_AR;
if($_SESSION['os']=='windows'){
	exi('','',SITEROOT.'/manage/index.php?action=user.login');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['role']=$_SESSION['role'];
$setting=$this->plugin_setting('setting');
$return['member']=array();
if($_RQ['op']=='main'){
	cfc('core')->checklogin();
	$this->model('model')->checkmember();
	$return['uid']=$_SESSION['uid'];
	$return['info']=array();
	$return['show']=0;
	$return['show2']=0;
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);	
	$params=array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
    $orderby='createtime desc';
	$total='';
	if($_RQ['lat']){
		$return['info']['lat']=cfc('tools')->Convert_GCJ02_To_BD09($_RQ['lat'],$_RQ['lng']);
		
	}
	$return['params']=array();
	$return['nomarkers']=array(
			'title'=>'附近暂无门店，叫洗车请拨'.$return['pluginset']['phone'],
			'iconPath'=>$return['pluginset']['map_icon']?tomedia($return['pluginset']['map_icon']):tomedia('../addons/jy_cxjxiche/resource/images/map_icon.png'),
			'width'=>36,
			'height'=>36,
			'callout'=>array(
				'content'=>'附近暂无门店，叫洗车请拨'.$return['pluginset']['phone'],
				'borderRadius'=>5,
				'bgColor'=>'#cdab6a',
				'color'=>'#fff',
				'textAlign'=>'center',
				'padding'=>5,
				'anchorY'=>0,
				'display'=>'ALWAYS',
				'borderColor'=>'#cdab6a',
			),
	);
	$return['isorder']=pdo_get('jy_cxjxiche_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1,6,7,8)));
	$return['isorder']=$return['isorder']?:array();
	$is_xieyi=pdo_getcolumn('jy_cxjxiche_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'is_xieyi');
	if(!$is_xieyi && $_SESSION['uid']){
		$return['xieyi']=$this->plugin_setting('daijia_xieyi')?:array();
		$return['xieyi']['title']='使用本洗车平台协议';
		$return['show_xieyi']=$return['xieyi']['content']?true:false;
	}
	//获取小程序订阅消息ID
	if($_SESSION['wxapp_id']){
		$tmplIds=array();
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('1290',[1,2,3,4],'create2buyer','新订单通知','jy_cxjxiche');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('205',[1,2,3],'get2user','洗车师傅接单通知','jy_cxjxiche');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('4968',[1,2,6,7,5],'pay2buyer','订单待支付通知','jy_cxjxiche');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	$groups=pdo_getall('jy_cxjxiche_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),'','','paixu desc, id desc');
	foreach($groups as &$gv){
		$gv['image']=tomedia($gv['image']);
	}
	$return['carnum']=pdo_count('jy_cxjxiche_mycar',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$return['params']['groupid']=$groups[0]['id']?:0;
	$return['groups']=$groups;
	$return['post']=array('goods'=>array(),'type'=>0,'day'=>date('Y-m-d'),'time'=>'11:00');
	$return['totalprice']=0.00;
	$return['total']=0;
	$days=array();
	for($i=0;$i<15;$i++){
		$days[]=date("Y-m-d",strtotime("+".$i." day"));
	}
	$return['days']=$days;
	$daytime=date('Y-m-d');
	$start = mktime(8, 0);
	$end = mktime(20, 0);
	$times=array();
	for($i = $start; $i < $end;) {
		$times[]=date('H:i', $i);
		$i += 3600;
	}
	$return['times']=$times;
	set_html('title',$return['pluginset']['title']?$return['pluginset']['title']:'在线服务');
	exi($return);
}
if($_RQ['op']=='pirce'){
	$post=get_postdata();
	$setting=$this->plugin_setting('setting');
	$distance=ceil($post['distance']/1000);
	$price=$setting['start'];
	if($distance>$setting['juli']){
		$num=ceil($distance/$setting['juli']);
		$price+=($num-1)*$setting['price1'];
	}
	$res=array(
		'price'=>$price,
	);
	exi($res);
}
if($_RQ['op']=='create'){
	$post=get_postdata();
	$data=array_fields(array('type','day','address','time','goods'),$post);
	$lat_lng=$this->session('lat_lng');
	$stores=$this->model('worker')->getstores($lat_lng);
	if(!$stores[0]){
		exi('附近暂无门店,暂时不能下单~','error');
	}
	$data['store_id']=$stores[0]['id'];
	$data['mid']=$_SESSION['uid'];
	if($post['name']&&$post['mobile']){
		$data['name']=$post['name'];	
		$data['mobile']=$post['mobile'];	
	}else{
		$member=pdo_get('jy_cxjxiche_member',array('mid'=>$_SESSION['uid']));
		$data['name']=$member['name'];	
		$data['mobile']=$member['mobile'];
	}
	$fees=$this->model('order')->get_fee($post['goods']);
	if(!$fees['total']){
		exi('请选择项目~','error');
	}
	$data=array_merge($data,$fees);
	$orderid=$this->model('order')->create($data);
	$data['id']=$orderid;
	$params=array(
		'id'=>$orderid,
		'title'=>'购买服务',
		'fee'=>$data['totalprice'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_cxjxiche/index/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_cxjxiche/index/',
		'error_url'=>$error_url?:'jy_cxjxiche/index/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$orderid));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$orderid,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	$res['orderid']=$orderid;
	$res['msg']='已下单，请耐心等待师傅接单';
	exi($res);
}
if($_RQ['op']=='getserve'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$groupid=$_RQ['groupid'];
	if($groupid){
		$params['groupid']=$groupid;
	}
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$orderby='paixu desc, id desc';
	$total='';
	$list=pdo_getslice('jy_cxjxiche_serve',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($list as &$item){
		$item['image']=$item['image']?tomedia($item['image']):'';
		$item['hdps']=json_decode($item['hdps'],true);
		foreach($item['hdps'] as &$hv){
			$hv=tomedia($hv);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getstores'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$list=$this->model('worker')->getstores($lat_lng,$_RQ['maxjuli'],$_RQ['limit'],$_RQ['page']);
	$markers=array();
	$return['pluginset']['map_icon']=$return['pluginset']['map_icon']?tomedia($return['pluginset']['map_icon']):tomedia('../addons/jy_cxjxiche/resource/images/map_icon.png');
	foreach($list as $k=>&$item){
		$name=cfc('tools')->substr($item['name'],0,1);
		$name .='师傅';
		$markers[]=array(
			'nowjuli'=>$item['nowjuli'],
			'latitude'=>$item['latlng']['lat'],
			'longitude'=>$item['latlng']['lng'],
			'title'=>$name,
			'iconPath'=>$item['logo']?tomedia($item['logo']):$return['pluginset']['map_icon'],
			'width'=>26,
			'height'=>26,
			'callout'=>array(
				'content'=>$name,
				'borderRadius'=>5,
				'bgColor'=>'#cdab6a',
				'color'=>'#fff',
				'textAlign'=>'center',
				'padding'=>5,
				'anchorY'=>0,
				'borderColor'=>'#cdab6a',
			),
		);
	}
	$return['list']=$markers;
	exi($return);
}
if($_RQ['op']=='get_isorder'){
	$isorder=pdo_get('jy_cxjxiche_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1,6,7,8)));
	$isorder=$isorder?:array('error'=>1);
	exi($return);
}
if($_RQ['op']=='setonline'){
	$post=get_postdata();
	$id=$post['id'];
	$busy=$post['busy'];
	$update=array('busy'=>$busy);
	if($busy==1){
		$update['lastontime']=TIMESTAMP;
	}
	pdo_update('jy_cxjxiche_worker',$update,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($busy==0){
		$this->model('worker')->up_dayonlinetime();
		exi('成功离线！');	
	}elseif($busy==1){
		exi('成功上线！');	
	}elseif($busy==2){
		exi('成功忙碌！');	
	}	 
}
if($_RQ['op']=='set_xieyi'){
	pdo_update('jy_cxjxiche_member',array('is_xieyi'=>1),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('你已同意本协议！');	
}
if($_RQ['op']=='get_fee'){
	$post=get_postdata();
	$serve_id=$post['serve_id'];
	$fees=$this->model('order')->get_fee($serve_id,0,$post['gocity']);
	exi($fees);	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_cxjxiche/order/');
	}
	$order=pdo_get('jy_cxjxiche_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_cxjxiche/order/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_cxjxiche/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_pay($order['id'],$params);
	exi('提交成功!');
}	
?>